﻿/*
   Author: Tomer Paz
   games.com
   2007
*/
// Players container. implements ICollection. a Singleton
package monopoly.Player {
	
	import monopoly.IFS.iterators.IIterator;
	import monopoly.IFS.iterators.ArrayIterator;
	import monopoly.IFS.Collections.ICollection;
	import monopoly.Player.Player;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class PlayersCollection extends EventDispatcher implements ICollection
		{
		private var _Players:Array;
		private var _PlastAction:Player;
		
		private static var _instance:PlayersCollection;

		public function PlayersCollection(parameter:SingletonEnforcer)
			{
			}

		public static function getInstance():PlayersCollection 
			{
         	if(_instance == null) 
				{
            	_instance = new PlayersCollection(new SingletonEnforcer());
				_instance._Players = new Array;
         		}
         	return PlayersCollection._instance;
      		}

		
		public function addPlayer(value:Player):void
			{
			if (null != value)
				_instance._Players.push(value);
			_PlastAction = value;
			
			dispatchEvent(new Event(Event.ADDED));
			}
		public function removePlayer(value:Player):void
			{
			var i:int = _instance._Players.indexOf(value);
			_instance._Players.splice(i,1);
			_PlastAction = value;
			
			dispatchEvent(new Event(Event.REMOVED));
			}
		public function get lastHandeledPlayer():Player
			{
			return (_PlastAction);
			}
			
		public function iterator(type:String = null):IIterator 
			{
	         return new ArrayIterator(_instance._Players);
			}
		
		public function getLength():int
			{
			return _instance._Players.length;
			}
		
		public function asArray():Array
			{
			var pArray:Array;
			pArray = _instance._Players.slice(0, this.getLength());
			return pArray;
			}
			
// operators?...		public function [](index:int)
		}
}

class SingletonEnforcer {}